package com.campus_assistant.controller.elasticsearch;

import com.alibaba.fastjson.JSON;
import com.campus_assistant.common.ToVue;
import com.campus_assistant.config.db.DynamicDataSource;
import com.campus_assistant.mapper.db1.CoursesMapper;
import com.campus_assistant.mapper.db1.ScoresMapper;
import com.campus_assistant.mapper.db1.StudentMapper;
import com.campus_assistant.pojo.db1.Courses;
import com.campus_assistant.pojo.db1.Scores;
import com.campus_assistant.pojo.db1.Student;
import com.campus_assistant.pojo.vo.StudentES;
import com.campus_assistant.pojo.vo.StudentScores;
import com.campus_assistant.service.db1.CoursesService;
import com.campus_assistant.service.db1.ScoresService;
import com.campus_assistant.service.db1.StudentScoresService;
import com.campus_assistant.service.db1.StudentService;
import com.campus_assistant.service.elasticsearch.ElasticSearchService;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class ElasticSearchController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private CoursesService coursesService;
    @Autowired
    private ScoresService scoresService;
    @Autowired
    private StudentScoresService studentScoresService;
    @Autowired
    private ElasticSearchService elasticSearchService;

    //更新ES
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    private CoursesMapper coursesMapper;
    @Autowired
    private ScoresMapper scoresMapper;
    @Autowired
    private StudentMapper studentMapper;

    @RequestMapping("/initStudent")
    public Boolean initStudent() throws IOException {//初始化学生表
        DynamicDataSource.dbName.set("db1");
        List<Student> stu_infoListAll = studentService.getStu_infoListAll();

        return elasticSearchService.initStudent(stu_infoListAll);
    }
    @RequestMapping("/initCourses")
    public Boolean initCourses() throws IOException {////初始化课程表
        DynamicDataSource.dbName.set("db1");
        List<Courses> courses = coursesService.selectAll();

        return elasticSearchService.initCourses(courses);
    }
    @RequestMapping("/initScores")
    public Boolean initScores() throws IOException {//初始化成绩表
        DynamicDataSource.dbName.set("db1");
        List<StudentScores> studentScores = studentScoresService.selectStudentScoresAll();
        return elasticSearchService.initScores(studentScores);
    }

    @RequestMapping("/searchStudentByNameES")
    public List<StudentES> searchStudentByNameES(@RequestParam("keyword")String keyword){//通过学生姓名查询相关学生信息
        DynamicDataSource.dbName.set("db1");
        try {
            updateESStudent();
            updateESCourse();
            updateESScores();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return elasticSearchService.searchStudentByNameES(keyword);
    }


    public void updateESCourse() throws IOException {//更新courses的ES索引
        DeleteIndexRequest studentDelete = new DeleteIndexRequest("courses");//删除索引
        try {
            restHighLevelClient.indices().delete(studentDelete, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        CreateIndexRequest studentCreate = new CreateIndexRequest("courses");//重新创建索引
        try {
            restHighLevelClient.indices().create(studentCreate,RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //重新开始插入数据
        List<Courses> coursesAll=coursesMapper.selectAll();
        BulkRequest bulkRequest=new BulkRequest();//插入数据
        bulkRequest.timeout("2m");//超时时间
        for (int i=0;i<coursesAll.size();i++){
            bulkRequest.add(new IndexRequest("courses")
                    .source(JSON.toJSONString(coursesAll.get(i)), XContentType.JSON));
        }
        BulkResponse bulk=restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);//执行数据插入
    }
    public void updateESScores() throws IOException {//更新Scores的ES索引
        DeleteIndexRequest studentDelete = new DeleteIndexRequest("scores");//删除索引
        try {
            restHighLevelClient.indices().delete(studentDelete, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        CreateIndexRequest studentCreate = new CreateIndexRequest("scores");//重新创建索引
        try {
            restHighLevelClient.indices().create(studentCreate,RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //重新开始插入数据
        List<Scores> scoresAll=scoresMapper.selectAll();
        BulkRequest bulkRequest=new BulkRequest();//插入数据
        bulkRequest.timeout("2m");//超时时间
        for (int i=0;i<scoresAll.size();i++){
            bulkRequest.add(new IndexRequest("scores")
                    .source(JSON.toJSONString(scoresAll.get(i)), XContentType.JSON));
        }
        BulkResponse bulk=restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);//执行数据插入
    }
    public void updateESStudent() throws IOException {//更新student的ES索引
        DeleteIndexRequest studentDelete = new DeleteIndexRequest("student");//删除索引
        try {
            restHighLevelClient.indices().delete(studentDelete, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        CreateIndexRequest studentCreate = new CreateIndexRequest("student");//重新创建索引
        try {
            restHighLevelClient.indices().create(studentCreate,RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //重新开始插入数据
        List<Student> stu_infoListAll=studentMapper.getStu_infoListAll();
        BulkRequest bulkRequest=new BulkRequest();//插入数据
        bulkRequest.timeout("2m");//超时时间
        for (int i=0;i<stu_infoListAll.size();i++){
            bulkRequest.add(new IndexRequest("student")
                    .source(JSON.toJSONString(stu_infoListAll.get(i)), XContentType.JSON));
        }
        BulkResponse bulk=restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);//执行数据插入
    }
}
